from paiban_new import Ui_MainWindow
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
import sys
import os
from PaiBanTools import PaiBan


class scheduling_window(QtWidgets.QMainWindow):
    def __init__(self):
        self.cwd = os.path.dirname(os.path.abspath(os.getcwd()))
        self.old_path = None

        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # 给button 的 点击动作绑定一个事件处理函数
        # 选择文件
        self.ui.pushButton.clicked.connect(self.slot_btn_chooseFile)

        self.ui.pushButton_2.clicked.connect(self.query_formula)

    def query_formula(self):
        # 此处编写具体的业务逻辑
        print("我被点击了")
        start = self.ui.dateEdit.date().toPyDate()
        end = self.ui.dateEdit_2.date().toPyDate()
        print("开始时间", start)
        print("结束时间", end)
        if self.old_path is None:
            QtWidgets.QMessageBox.question(self,
                                           '信息',
                                           '请选择文件',
                                           QMessageBox.Yes | QMessageBox.No,
                                           QMessageBox.Yes)
            return

        try:
            pb = PaiBan(path=self.old_path, start=start, end=end)
            pb.run()
        except Exception as e:
            print("创建失败！", e)
            self.ui.textEdit.append(f"<h3>创建失败！失败原因：{e}</h3>")
        else:
            print("新建排班表:{}成功~".format(pb.new_path))
            self.ui.textEdit.append(f"<h3>根据开始时间: {start} 和 结束时间: {end}</h3>")
            self.ui.textEdit.append(f"<h3 style='color:red'>新建排班表: {pb.new_path}成功~</h3>")
            QtWidgets.QMessageBox.question(self,
                                           '信息',
                                           '新建排班表成功~~',
                                           QMessageBox.Yes | QMessageBox.No,
                                           QMessageBox.Yes)

            self.slot_btn_chooseDir()

    def slot_btn_chooseFile(self):

        fileName_choose, filetype = QtWidgets.QFileDialog.getOpenFileName(self,
                                                                          "选取文件",
                                                                          self.cwd,  # 起始路径
                                                                          "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,用双分号间隔

        if fileName_choose == "":
            print("\n取消选择")
            return

        print("\n你选择的文件为:")
        print(fileName_choose)
        self.old_path = fileName_choose
        print("文件筛选器类型: ", filetype)
        self.ui.textEdit.append(f"<h3>选择文件: {fileName_choose}</h3>")

    def slot_btn_chooseDir(self):
        fileName_choose, filetype = QtWidgets.QFileDialog.getOpenFileName(self,
                                                                          "选取文件",
                                                                          os.path.join(self.cwd, "paiban"),  # 起始路径
                                                                          "All Files (*);;Text Files (*.txt)")

        if fileName_choose == "":
            print("\n取消选择")
            return

        print("\n你选择的文件为:")
        print(fileName_choose)
        print("文件筛选器类型: ", filetype)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = scheduling_window()
    window.show()
    sys.exit(app.exec_())
